package demo.bocweb.login_module.presenter;

import demo.bocweb.com.sdk.base.bean.BaseBean;
import demo.bocweb.com.sdk.baseApi.AppUrl;
import demo.bocweb.com.sdk.baseApi.BaseErrorObserver;
import demo.bocweb.com.sdk.baseApi.BaseObserver;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.ToastyUtil;
import demo.bocweb.login_module.contract.ResetPhoneContract;
import demo.bocweb.login_module.model.ResetPhoneModel;
import okhttp3.RequestBody;

/**
 * Created by zhelun on 2018/2/3.
 */

public class ResetPhonePresenter extends ResetPhoneContract.IResetPhonePresenter {
    @Override
    public ResetPhoneContract.IResetPhoneModel getModel() {
        return ResetPhoneModel.newInstance();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void sendSmaCode(RequestBody body) {
        if (mIModel == null || mIView == null)
            return;
        mRxManager.register(mIModel.sendSmaCode(body)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean baseBean) {
                        Loading.dismiss();
                        if (baseBean.getReturnCode() == AppUrl.SuccessCode) {
                            if (mIView == null) {
                                return;
                            }
                            mIView.sendSmaCodeSuccess();
                        } else {
                            ToastyUtil.showError(baseBean.getMsg());
                        }
                    }
                }, new BaseErrorObserver()));
    }

    @Override
    public void checkSmaCode(RequestBody body) {
        if (mIModel == null || mIView == null)
            return;
        mRxManager.register(mIModel.checkSmaCode(body)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean baseBean) {
                        Loading.dismiss();
                        if (baseBean.getReturnCode() == AppUrl.SuccessCode) {
                            if (mIView == null) {
                                return;
                            }
                            mIView.checkSmaCodeSuccess();
                        } else {
                            ToastyUtil.showError(baseBean.getMsg());
                        }
                    }
                }, new BaseErrorObserver()));
    }

    @Override
    public void resetPhone(RequestBody body) {
        if (mIModel == null || mIView == null)
            return;
        mRxManager.register(mIModel.resetPhone(body)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean baseBean) {
                        Loading.dismiss();
                        if (baseBean.getReturnCode() == AppUrl.SuccessCode) {
                            if (mIView == null) {
                                return;
                            }
                            mIView.resetPhoneSuccess();
                        } else {
                            ToastyUtil.showError(baseBean.getMsg());
                        }
                    }
                }, new BaseErrorObserver()));
    }
}
